package ouyuwang.gpsbox;

import android.app.Activity;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.location.LocationProvider;
import android.os.Bundle;
import android.util.Log;

import java.util.Random;

import static android.content.ContentValues.TAG;

/**
 * Created by yupeng on 17/07/20.
 */

public class LocationHandler implements LocationListener {

    private Main activity = null;

    public LocationHandler(Main activity) {
        this.activity = activity;
    }

    /**
     * 位置信息变化时触发
     */
    public void onLocationChanged(Location location) {
        //测试使用
//        location.setSpeed(new Random().nextInt(100));
        activity.updateLocation(location);
        activity.updateMessage("GPS已更新");
    }

    /**
     * GPS状态变化时触发
     */
    public void onStatusChanged(String provider, int status, Bundle extras) {
        switch (status) {
            // GPS状态为可见时
            case LocationProvider.AVAILABLE:
                activity.updateMessage("当前GPS状态为可见状态");
                break;
            // GPS状态为服务区外时
            case LocationProvider.OUT_OF_SERVICE:
                activity.updateMessage("当前GPS状态为服务区外状态");
                break;
            // GPS状态为暂停服务时
            case LocationProvider.TEMPORARILY_UNAVAILABLE:
                activity.updateMessage("当前GPS状态为暂停服务状态");
                break;
        }
    }

    /**
     * GPS开启时触发
     */
    public void onProviderEnabled(String provider) {
        Location location = activity.getLocationManager().getLastKnownLocation(provider);
        activity.updateLocation(location);
    }

    /**
     * GPS禁用时触发
     */
    public void onProviderDisabled(String provider) {
        activity.updateLocation(null);
    }
}
